package com.bqtt.tumor.demo.business;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.bqtt.tumor.demo.common.MyBeanFactory;
import com.bqtt.tumor.demo.common.utility.InlineTools;
import com.bqtt.tumor.demo.data_access.CityDAO;
import com.bqtt.tumor.demo.data_access.DeseaseDAO;
import com.bqtt.tumor.demo.entity.City;
import com.bqtt.tumor.demo.entity.Desease;
import com.bqtt.tumor.demo.entity.Feature;

public class DeseaseService {
	
	private static DeseaseDAO deseaseDAO = (DeseaseDAO) MyBeanFactory.getBean("deseaseDAO");
	private static Logger log = LoggerFactory.getLogger(DeseaseService.class);
	
	public static List<Desease> getDeseaseList()
	{
		return deseaseDAO.findAll();
	}
	
	
	public static String getDeseaseNameByCode(String code)
	{
		List<Desease> result = deseaseDAO.findByProperty("desease_code", code);
		
		if(result!=null && !result.isEmpty())
		{
			return result.get(0).getDesease_name();
		}
		return "";
	}
	
	public static Desease getDeseaseByCode(String code)
	{
		List<Desease> result = deseaseDAO.findByProperty("desease_code", code);
		
		if(result!=null && !result.isEmpty())
		{
			return result.get(0);
		}
		return null;
	}
	
	
	public static List<Desease> searchByCondition(Desease condition,int pageSize,int pageNo)
	{
		
		List<Desease> result = deseaseDAO.fuzzyFindByProperties(condition.getSQLValidAttributes(),pageSize,pageNo);
		for(Desease item:result)
		{
			item.fillStringField();
		}
		return result;
	}
	
	public static List<Desease> searchByCondition(Desease condition,int pageSize,int pageNo,long id)
	{
		
		List<Desease> result = deseaseDAO.fuzzyFindByProperties(condition.getSQLValidAttributes(),pageSize,pageNo);
		
		for(Desease item:result)
		{
			if(item.getDesease_def_id()==id)
			{
				item.setDisplayName("inline-block");
			}
			item.fillStringField();
		}
		return result;
	}
	
	public static List<Integer> getFeatureIdsByDeseaseCode(String code)
	{
		List<Integer> result = new ArrayList<Integer>();
		
		Desease temp = getDeseaseByCode(code);
		
		if(temp==null)
		{
		   return result;
		}
		
		String methods = temp.getClinic_feature();
				
				
		if(!InlineTools.isEmpty(methods))
		{
			String [] ids = methods.split("\\|");
			for(String item:ids)
			{
				try{
					result.add(Integer.parseInt(item));
				}
				catch(Exception e)
				{
					;
				}
			}
		}
		return result;
	}
	
	public static List<Integer> getTreatmentMethodIdsByDeseaseCode(String code)
	{
		List<Integer> result = new ArrayList<Integer>();
		
		Desease temp = getDeseaseByCode(code);
		
		if(temp==null)
		{
		   return result;
		}
		
		String methods = temp.getTreatment_methods();
				
				
		if(!InlineTools.isEmpty(methods))
		{
			String [] ids = methods.split("\\|");
			for(String item:ids)
			{
				try{
					result.add(Integer.parseInt(item));
				}
				catch(Exception e)
				{
					;
				}
			}
		}
		return result;
	}
	
	public static boolean add(Desease obj)
	{
		try
		{
			deseaseDAO.save(obj);
			return true;
		}
		catch(Exception e)
		{
			log.error(e.getMessage());
	     	return false;	
		}
		
	}
	
	public static boolean update(Desease obj)
	{
		try
		{
			 
			if(obj.getDesease_def_id()>0)
			{
				deseaseDAO.update(obj);
				return true;
			}
			else
				
			return false;
		}
		catch(Exception e)
		{
			log.error(e.getMessage());
	     	return false;	
		}
		
	}
}
